import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/components/Login'
import ComplaintList from '@/components/ComplaintList'
import ComplaintAdd from '@/components/ComplaintAdd'

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/complaint/list',
      name: 'ComplaintList',
      component: ComplaintList
    },
    {
      path: '/complaint/create',
      name: 'ComplaintAdd',
      component: ComplaintAdd
    }
  ]
})

// 添加路由导航守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  if (to.path === '/login') {
    next()
  } else {
    if (token) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
